﻿using Meiam.System.Interfaces;
using Meiam.System.Interfaces.IService;
using Meiam.System.Model.Entity;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Meiam.System.Model.Dto;
using Microsoft.Extensions.Configuration;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using SqlSugar;
using System.Text;
using System.Data;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Meiam.System.Model;
using Microsoft.Extensions.Logging;


namespace Meiam.System.Hostd.Controllers.Gokin
{
    /// <summary>
    /// TMS车牌账号角色校验
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class TMSUserRoleInFoCheckController : ControllerBase
    {
        private readonly ILogger<TMSUserRoleInfoCheckRequestDto> _logger;
        public readonly IWMSTokenCheckService _WMSTokenCheckService;
        public readonly string con;


        public TMSUserRoleInFoCheckController(ILogger<TMSUserRoleInfoCheckRequestDto> logger,IWMSTokenCheckService wmsTokenCheckService, IConfiguration _configuration)
        {
            _logger = logger;
            _WMSTokenCheckService = wmsTokenCheckService;
             con= _configuration.GetConnectionString("DDM_DbConnection_ZH");

        }

        /// <summary>
        /// TMS车牌账号角色校验
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public IActionResult PostTMSUserRoleInFoCheck([FromBody] TMSUserRoleInfoCheckRequestDto strparm)
        {
            string  strMsg = "";
            object log = "";
            log = strparm;
            StringBuilder strSql = new StringBuilder();
            List<string> strSqlList = new List<string>();
            TMSUserRoleInfoCheckResponseDto re = new TMSUserRoleInfoCheckResponseDto();
            string telphone = strparm.telphone;
            string carno = strparm.carno;

            try
            {
                strSql = new StringBuilder();
                strSql.Append(@"select u.id,u.account,u.username,r.param_values  from  xdap_users u inner join apaas_role_user_params r
on u.id=r.user_id  where r.role_id='383589103830040576' and  u.account='" + telphone + "' and r.param_values like '%"+ carno + "%'");
                DataTable dt_info = DB.Query(strSql.ToString(), con);



                if (dt_info.Rows.Count<=0)
                {
                    strMsg = "手机号：<"+ telphone + ">账号，车牌<"+ carno +">找不到相关账号信息，请检查车牌角色是否异常，或车辆申请审批状态！";
                }           

            }
            catch (Exception ex)
            {
                strMsg = ex.ToString();
            }

         


            if (strMsg=="")
            {
                re.status = "成功";
                re.code = "0";
            }
            else
            {
                re.status = "失败";
                re.code = "500";
                re.message = strMsg;
            }


            return new JsonResult(re);
        }

      
    }
}
